package au.com.finalconcept.roo.addon.behaviours;

import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.Service;
import org.springframework.roo.model.JavaType;
import org.springframework.roo.shell.CliAvailabilityIndicator;
import org.springframework.roo.shell.CliCommand;
import org.springframework.roo.shell.CliOption;
import org.springframework.roo.shell.CommandMarker;

/**
 * Sample of a command class. The command class is registered by the Roo shell
 * following an automatic classpath scan. You can provide simple user
 * presentation-related logic in this class. You can return any objects from
 * each method, or use the logger directly if you'd like to emit messages of
 * different severity (and therefore different colours on non-Windows systems).
 * 
 * @since 1.1
 */
@Component
// Use these Apache Felix annotations to register your commands class in the Roo
// container
@Service
public class BehavioursCommands implements CommandMarker { // All command types
                                                           // must implement the
                                                           // CommandMarker
                                                           // interface

    /**
     * Get a reference to the BehavioursOperations from the underlying OSGi
     * container
     */
    @Reference
    private BehavioursOperations operations;

    /**
     * This method is optional. It allows automatic command hiding in situations
     * when the command should not be visible. For example the 'entity' command
     * will not be made available before the user has defined his persistence
     * settings in the Roo shell or directly in the project.
     * 
     * You can define multiple methods annotated with
     * {@link CliAvailabilityIndicator} if your commands have differing
     * visibility requirements.
     * 
     * @return true (default) if the command should be visible at this stage,
     *         false otherwise
     */
    @CliAvailabilityIndicator({ "behaviours setup", "behaviours soft-delete add", "behaviours soft-delete all" })
    public boolean isCommandAvailable() {
        return operations.isCommandAvailable();
    }

    /**
     * This method registers a command with the Roo shell. It also offers a
     * mandatory command attribute.
     * 
     * @param type
     */
    @CliCommand(value = "behaviours soft-delete add", help = "Adds the RooSoftDelete annotation to your entity")
    public void softDeleteAdd(@CliOption(key = "type", mandatory = true, help = "The java type to apply this annotation to") JavaType target) {
        operations.annotateSoftDeleteType(target);
    }

    /**
     * This method registers a command with the Roo shell. It has no command
     * attribute.
     * 
     */
    @CliCommand(value = "behaviours soft-delete all", help = "Adds the RooTimestampable annotation to all your entity types")
    public void softDeleteAll() {
        operations.annotateSoftDeleteAll();
    }

    /**
     * This method registers a command with the Roo shell. It also offers a
     * mandatory command attribute.
     * 
     * @param type
     */
    @CliCommand(value = "behaviours timestampable add", help = "Adds the RooTimestampable annotation to your entity")
    public void timestampableAdd(
            @CliOption(key = "type", mandatory = true, help = "The java type to apply this annotation to") JavaType target) {
        operations.annotateTimestampableType(target);
    }

    /**
     * This method registers a command with the Roo shell. It has no command
     * attribute.
     * 
     */
    @CliCommand(value = "behaviours timestampable all", help = "Adds the RooTimestampable annotation to all your entity types")
    public void timestampableAll() {
        operations.annotateTimestampableAll();
    }

    /**
     * This method registers a command with the Roo shell. It also offers a
     * mandatory command attribute.
     * 
     * @param type
     */
    @CliCommand(value = "behaviours slugable add", help = "Adds the RooSlugable annotation to your entity")
    public void slugableAdd(@CliOption(key = "type", mandatory = true, help = "The java type to apply this annotation to") JavaType target,
            @CliOption(key = "fields", mandatory = true, help = "A comma seperated list of field names defined on your "
                    + "class the build the Slug from. Note the casing of the field names must match what is defined in your "
                    + "class file") String fields) {
        operations.annotateSlugableType(target, fields);
    }

    /**
     * This method registers a command with the Roo shell. It has no command
     * attribute.
     * 
     */
    @CliCommand(value = "behaviours setup", help = "Adds the required dependancies to your project")
    public void setup() {
        operations.setup();
    }

}